/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import mx.edu.itschapala.sistemas.septimo.control.*;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author Ingenieriaz
 */
public class Registrar extends javax.swing.JFrame {

    /**
     * Creates new form Registrar
     */
    private ProductoLNLocal productoControl;
    private TipoVentana tipoVentana;
    private Producto producto;

    public Registrar(Producto producto,ProductoLNLocal productoControl, TipoVentana tipoVentana) {
        this.producto=producto;
        this.productoControl = productoControl;
        this.tipoVentana = tipoVentana;
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                titulo.setText("Registrar Producto");
                break;
            case VENTANA_BUSCAR:
                titulo.setText("Buscar por ID");
                botonGuardar.setText("Aceptar");
                textNombre.setEditable(false);
                textPrecio.setEditable(false);
                textDepart.setEditable(false);
                textDescripcion.setEditable(false);
                botonCancelar.setVisible(false);
                break;
            case VENTANA_MODIFICAR:
                titulo.setText("Modificar Producto");
                //botonGuardar.setText("Buscar");
                textCodigo.setEditable(false);
                textCodigo.setText(producto.getCodigo()+"");
                textNombre.setText(producto.getNombre());
                textPrecio.setText(producto.getPrecio()+"");
                textDepart.setText(producto.getDepartamento());
                textDescripcion.setText(producto.getDescripcion());
                //botonCancelar.setVisible(false);
            case VENTANA_ELIMINAR:
                titulo.setText("Eliminar producto");
                textCodigo.setEditable(false);
                textCodigo.setText(producto.getCodigo()+"");
                textNombre.setText(producto.getNombre());
                textPrecio.setText(producto.getPrecio()+"");
                textDepart.setText(producto.getDepartamento());
                textDescripcion.setText(producto.getDescripcion());
                botonGuardar.setText("Eliminar");
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        titulo = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        botonGuardar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textCodigo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        textNombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textPrecio = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        textDepart = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        textDescripcion = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setResizable(false);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mx/edu/itschapala/sistemas/septimo/imgs/abstract_3d_cubes.jpg"))); // NOI18N

        jPanel6.setOpaque(false);

        jPanel5.setOpaque(false);

        titulo.setFont(new java.awt.Font("Times New Roman", 0, 36)); // NOI18N
        titulo.setForeground(new java.awt.Color(204, 204, 204));
        titulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titulo.setText("Producto");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(titulo, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(titulo)
        );

        jPanel4.setOpaque(false);

        jPanel3.setOpaque(false);

        botonGuardar.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonCancelar.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(botonGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addComponent(botonCancelar)
                .addGap(26, 26, 26))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonGuardar)
                    .addComponent(botonCancelar))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridLayout(6, 2, 0, 4));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(204, 204, 204));
        jLabel2.setText("Codigo:");
        jPanel1.add(jLabel2);

        textCodigo.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        textCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textCodigoKeyPressed(evt);
            }
        });
        jPanel1.add(textCodigo);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(204, 204, 204));
        jLabel3.setText("Nombre");
        jPanel1.add(jLabel3);

        textNombre.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jPanel1.add(textNombre);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(204, 204, 204));
        jLabel4.setText("Precio");
        jPanel1.add(jLabel4);

        textPrecio.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jPanel1.add(textPrecio);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(204, 204, 204));
        jLabel5.setText("Departamento");
        jPanel1.add(jLabel5);

        textDepart.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jPanel1.add(textDepart);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(204, 204, 204));
        jLabel6.setText("Descripcion");
        jPanel1.add(jLabel6);

        textDescripcion.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jPanel1.add(textDescripcion);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed

          switch (tipoVentana) {
            case VENTANA_REGISTRO:
                    Producto p = new Producto();
                    p.setCodigo(Integer.parseInt(textCodigo.getText()));
                    p.setNombre(textNombre.getText());
                    p.setPrecio(Float.valueOf(textPrecio.getText()));
                    p.setDepartamento(textDepart.getText());
                    p.setDescripcion(textDescripcion.getText());
                    productoControl.registrar(p);
                    vaciarCampos();
                    JOptionPane.showMessageDialog(null, "Guardado correctamente");
                break;
            case VENTANA_BUSCAR:
                this.dispose();
                break;
            
            case VENTANA_MODIFICAR:
                    p = new Producto();
                    p.setCodigo(Integer.parseInt(textCodigo.getText()));
                    p.setNombre(textNombre.getText());
                    p.setPrecio(Float.valueOf(textPrecio.getText()));
                    p.setDepartamento(textDepart.getText());
                    p.setDescripcion(textDescripcion.getText());
                    productoControl.modificar(p);
                    vaciarCampos();
                    textNombre.setEditable(false);
                    textPrecio.setEditable(false);
                    textDepart.setEditable(false);
                    textDescripcion.setEditable(false);
                    JOptionPane.showMessageDialog(null, "Modificado correctamente");
                    this.dispose();
                break;
             
            case VENTANA_ELIMINAR:
                    p = new Producto();
                    p.setCodigo(Integer.parseInt(textCodigo.getText()));
                    p.setNombre(textNombre.getText());
                    p.setPrecio(Float.valueOf(textPrecio.getText()));
                    p.setDepartamento(textDepart.getText());
                    p.setDescripcion(textDescripcion.getText());
                    productoControl.eliminar(p);
                vaciarCampos();
                JOptionPane.showMessageDialog(null, "Eliminado correctamente");
                this.dispose();
                //this.dispose();
                break;
                
            
        }

    }//GEN-LAST:event_botonGuardarActionPerformed

    private void textCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textCodigoKeyPressed
        if(tipoVentana!=tipoVentana.VENTANA_REGISTRO){
           if(evt.getKeyCode()==KeyEvent.VK_ENTER){
               Producto p=productoControl.buscarPorCodigo(Integer.parseInt(textCodigo.getText()));
               if(p!=null){
                   textNombre.setText(p.getNombre());
                   textPrecio.setText(String.valueOf(p.getPrecio()));
                   textDepart.setText(p.getDepartamento());
                   textDescripcion.setText(p.getDepartamento());
                   if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                       textNombre.setEditable(true);
                       textPrecio.setEditable(true);
                       textDepart.setEditable(true);
                       textDescripcion.setEditable(true);
                   }
                   if(tipoVentana==TipoVentana.VENTANA_ELIMINAR){
                       textNombre.setText(p.getNombre());
                       textPrecio.setText(String.valueOf(p.getPrecio()));
                       textDepart.setText(p.getDepartamento());
                       textDescripcion.setText(p.getDepartamento());
                       
                       
                   }
               }
           }
       }
    }//GEN-LAST:event_textCodigoKeyPressed

    private void vaciarCampos(){
        textCodigo.setText("");
        textNombre.setText("");
        textPrecio.setText("");
        textDepart.setText("");
        textDescripcion.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField textCodigo;
    private javax.swing.JTextField textDepart;
    private javax.swing.JTextField textDescripcion;
    private javax.swing.JTextField textNombre;
    private javax.swing.JTextField textPrecio;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables
}
